package com.qall.util.sql;


import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

public class GeneratorSqlUtil {


    /**
     * 获取该类对应的sql建表语句
     *
     * @param tClass
     * @return
     */
    public static String generatorCreateTableSql(Class<?> tClass) {
        StringBuilder createTableSql = new StringBuilder();
        SqlTable sqlTable = tClass.getAnnotation(SqlTable.class);
        if (sqlTable == null) {
            return "";
        }

        String tableName = sqlTable.tableName();
        if (tableName.equals("")) {
            tableName = getSqlName(tClass.getSimpleName());
        }
        createTableSql.append("CREATE TABLE IF NOT EXISTS ").append(tableName).append(" ( \n");
        Field[] fields = tClass.getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            Field field = fields[i];

            SqlColumn sqlColumn = field.getAnnotation(SqlColumn.class);
            if (sqlColumn != null) {
                sqlColumn.colType();
                String colName = sqlColumn.colName();
                if (colName.equals("")) {
                    colName = getSqlName(field.getName());
                }
                String colType = sqlColumn.colType();
                if (colType.equals("")) {
                    colType = getType(field.getType().getTypeName());
                }
                String sqlCol = " " + colName + " " + colType;

                if (sqlColumn.isPrimary()) {
                    sqlCol = sqlCol + " UNSIGNED AUTO_INCREMENT";
                }
                if (!sqlColumn.nullAble()) {
                    sqlCol = sqlCol + " NOT";
                }
                sqlCol = sqlCol + " NULL";
                sqlCol = sqlCol + " comment";
                sqlCol = sqlCol + " '";
                sqlCol = sqlCol + sqlColumn.comment();
                sqlCol = sqlCol + "' ,\n";
                createTableSql.append(sqlCol);
            }
        }
        createTableSql.append("PRIMARY KEY (").append(sqlTable.primaryKeyName()).append(")\n");
        createTableSql.append(")").append("ENGINE=InnoDB DEFAULT CHARSET=utf8;");
        return createTableSql.toString();
    }

    private static String getSqlName(String javaName) {
        return "";
    }

    private static String getType(String javaType) {

        Map<String, String> typeMap = new HashMap<>();
        typeMap.put("boolean", "TINYINT");
        typeMap.put("Boolean", "TINYINT");
        typeMap.put("byte", "TINYINT");
        typeMap.put("Byte", "TINYINT");
        typeMap.put("char", "varchar(1)");
        typeMap.put("short", "SMALLINT");
        typeMap.put("Short", "SMALLINT");
        typeMap.put("int", "INT");
        typeMap.put("Integer", "INT");
        typeMap.put("float", "FLOAT");
        typeMap.put("Float", "FLOAT");
        typeMap.put("long", "BIGINT");
        typeMap.put("Long", "BIGINT");
        typeMap.put("double", "DOUBLE");
        typeMap.put("Double", "DOUBLE");
        typeMap.put("String", "varchar(128)");
        return typeMap.get(javaType);

    }


    public static void main(String[] args) {
        String ret = generatorCreateTableSql(TestEntity.class);
        System.out.println(ret);
    }
}
